package hsl.study.v1;


import java.util.concurrent.TimeUnit;

/**
 * @author HSL
 * @version 1.0
 * @data 2025/11/17 9:58
 * @desc 测试类
 */

public class ProxyTest {

    public static void main(String[] args) {
        // 创建目标对象
        Subject realSubject = new RealSubject();
        //创建代理对象
        ProxyHandler handler = new ProxyHandler(realSubject);

        Subject proxySubject = (Subject) handler.getProxyInstance();

        // 调用方法1
        proxySubject.doSomething();
        System.out.println("-----------------------------------------");
        // 调用方法2
        String result = proxySubject.sayHello("lhr");
        System.out.println("[返回结果]" + result);
        System.out.println("-----------------------------------------");
        System.out.println("======================以下是动态代理总结====================");

        // 暂停几秒线程
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


        String note = """
                动态代理优点,
                ------------------------
                无需修改原类即可增强功能(AOP思想);
                可统一处理日志、事务、安全等横切逻辑;
                仅支持接口代理(JDK Proxy)、如果想代理类可用、CGLIB
                ------------------------
                """;
        System.out.println(note);
    }
}
